import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.*;

public class userInterface extends JFrame
{
	project3 p3;

	private JFrame f1 = new JFrame();
	private JFrame f2 = new JFrame();;
	private JPanel loginPanel0 = new JPanel(new GridLayout(3, 1));
	private JPanel loginPanel = new JPanel(new GridLayout(2, 2));
	private JPanel loginPanel2 = new JPanel(new GridLayout(1, 2));
	private JPanel loginPanel3 = new JPanel(new GridLayout(1, 2));
	private JPanel studentPanel = new JPanel(new GridLayout(7, 1));
	private JPanel facultyPanel = new JPanel(new GridLayout(9, 1));
	private JLabel useridLabel = new JLabel("User ID");
	private JLabel passwordLabel = new JLabel("Password");
	private TextField useridInput = new TextField(10);
	private JPasswordField passwordInput = new JPasswordField(10);
	private JRadioButton facultyButton = new JRadioButton("Faculty");
	private JRadioButton studentButton = new JRadioButton("Student");
	private JButton okButton = new JButton("OK");
	private JButton cancelButton = new JButton("Cancel");
	// student panel
	private JButton myMessageButton = new JButton("My Messages");
	private JButton CalendarofevalutionsButton = new JButton(
			"Calendar of evalutions");
	private JButton MyClassesButton = new JButton("My Classes");
	private JButton MygradesButton = new JButton("My grades");
	private JLabel useridButton = new JLabel();
	private JLabel availableToolsButton = new JLabel("Available Tools");
	// faculty panel
	private JButton createClassButon = new JButton("Create a class");
	private JButton modifyClassButon = new JButton("Modify a class");
	private JButton assignStudentButon = new JButton("Register a student");
	private JButton createEvaluationButon = new JButton("Create a evaluation");
	private JButton modifyEvaluationButon = new JButton("Modify a evaluation");
	private JButton reportClassButon = new JButton("Report of a class");
	private JButton reportStudentAndGradeButon = new JButton(
			"Report of students and grades");
	private JButton exitButton = new JButton("Exit the system");
	private ButtonGroup group = new ButtonGroup();

	// temp frame
	// JFrame tempFrame;
	// Button
	int facultyButtonClicked = -1;
	int studentButtonClicked = -1;

	// current user
	String curr_id = null;

	public userInterface()
	{
		p3 = new project3();
	}

	public void setInterface()
	{
		f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f1.setSize(500, 500);
		f1.setVisible(true);
		f1.setLocationRelativeTo(null);
		f1.setTitle("Purdue-CT");

		loginPanel.add(useridLabel);
		loginPanel.add(useridInput);
		loginPanel.add(passwordLabel);
		loginPanel.add(passwordInput);
		loginPanel0.add(loginPanel);

		facultyButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				facultyButtonClicked = 1;
				studentButtonClicked = -1;
				// System.out.println("facultyButtonClicked");
			}

		});
		studentButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				facultyButtonClicked = -1;
				studentButtonClicked = 1;
				// System.out.println("studentButtonClicked");
			}

		});
		group.add(facultyButton);
		group.add(studentButton);
		loginPanel2.add(facultyButton);
		loginPanel2.add(studentButton);
		loginPanel0.add(loginPanel2);

		okButton.addActionListener(new ActionListener()
		{
			
			public void actionPerformed(ActionEvent e)
			{
				String tempUserId = new String(useridInput.getText());
				String tempPassword = new String(passwordInput.getPassword());
				
				/*
				if(tempUserId.equals("")){
					JOptionPane.showMessageDialog(null,
						    "User name can not be empty!",
						    "Inane error",
						    JOptionPane.ERROR_MESSAGE);
					
				}
				else if(tempPassword.equals("")){
					JOptionPane.showMessageDialog(null,
						    "User password can not be empty!",
						    "Inane error",
						    JOptionPane.ERROR_MESSAGE);
					
				}
				else if(facultyButtonClicked== -1 && studentButtonClicked == -1){
		
					JOptionPane.showMessageDialog(null,
						    "Please select a user tpye!",
						    "Inane error",
						    JOptionPane.ERROR_MESSAGE);
					
				}
				else*/ if (facultyButtonClicked == 1)
				{
					if (p3.verifyFaculty(tempUserId, tempPassword) == 1)
					{
						curr_id = tempUserId;
						JOptionPane.showMessageDialog(null,
								"Successfully login as faculty number: "
										+ tempUserId);
						f1.dispose();
						facultyInterface();
					}/* else
					{
						JOptionPane
								.showMessageDialog(null,
										"Invalid user name or password, please try again!");
					}*/

				} else if (studentButtonClicked == 1)
				{
					if (p3.verifyStudent(tempUserId, tempPassword) == 1)
					{
						curr_id = tempUserId;
						JOptionPane.showMessageDialog(null,
								"Successfully login as student number: "
										+ tempUserId);
						f1.dispose();
						studentInterface();
					}/* else
					{
						JOptionPane
								.showMessageDialog(null,
										"Invalid user name or password, please try again!", "Inane error",JOptionPane.ERROR_MESSAGE);
					}*/
				} 
				
				
				// p3.myClasses();
			}
		});
		cancelButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				/*
				f1.dispose();
				try
				{
					p3.conn.close();
					p3.stmt.close();
				} catch (SQLException e1)
				{
					// TODO Auto-generated catch block
					System.out
							.println("Database errors, did not close the connection");
				}
				*/
			}
		});

		loginPanel3.add(okButton);
		loginPanel3.add(cancelButton);
		loginPanel0.add(loginPanel3);
		f1.add(loginPanel0);
		f1.pack();
	}

	public void facultyInterface()
	{
		System.out.println("faculty");
		f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f2.setSize(500, 500);
		f2.setVisible(true);
		f2.setTitle("Purdue-CT");
		f2.setLocationRelativeTo(null);
		useridButton.setText("Welcome " + p3.getFacultyName(curr_id));

		createClassButon.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				// Interface to create a new class
				final JFrame tempFrame = new JFrame();
				tempFrame.setSize(600, 200);
				tempFrame.setVisible(true);
				tempFrame.setTitle("Purdue-CT");
				tempFrame.setLocationRelativeTo(null);
				JLabel classNameLabel = new JLabel("Class name: ");
				JLabel semesterLabel = new JLabel("Semester: ");
				JLabel yearLabel = new JLabel("Year: ");
				JLabel meetsAtLabel = new JLabel("Meet At: (mm,dd)");
				JLabel roomLabel = new JLabel("Room: ");
				final JTextField classNameField = new JTextField();
				final JTextField semesterField = new JTextField();
				final JTextField yearField = new JTextField();
				final JTextField meetsAtField = new JTextField();
				final JTextField roomField = new JTextField();
				JPanel classPanel = new JPanel(new GridLayout(6, 2));
				JButton createButton = new JButton("Create");
				JButton cancelButton = new JButton("Cancel");

				createButton.addActionListener(new ActionListener()
				{
					
					public void actionPerformed(ActionEvent e)
					{
						/*if (!classNameField.getText().equals(null)
								&& !semesterField.getText().equals(null)
								&& !yearField.getText().equals(null)
								&& !meetsAtField.getText().equals(null)
								&& !roomField.getText().equals(null))*/
							if(p3.createClass(classNameField.getText(),
									semesterField.getText(),
									yearField.getText(),
									meetsAtField.getText(),
									roomField.getText(), curr_id)== 1 ){
						JOptionPane.showMessageDialog(null,
								"Successfully created a new class!");
						tempFrame.dispose();
							}else{
								JOptionPane
								.showMessageDialog(null,
										"Create class failed, please check and try again!",
									    "Inane error",
									    JOptionPane.ERROR_MESSAGE);
							}
					}
				});
				cancelButton.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						JOptionPane
								.showMessageDialog(null,
										"Successfully exit without create a new class!");
						/*tempFrame.dispose();*/
					}
				});
				classPanel.add(classNameLabel);
				classPanel.add(classNameField);
				classPanel.add(semesterLabel);
				classPanel.add(semesterField);
				classPanel.add(yearLabel);
				classPanel.add(yearField);
				classPanel.add(meetsAtLabel);
				classPanel.add(meetsAtField);
				classPanel.add(roomLabel);
				classPanel.add(roomField);
				classPanel.add(createButton);
				classPanel.add(cancelButton);
				tempFrame.add(classPanel);
				tempFrame.pack();

			}
		});

		modifyClassButon.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				int classCount = p3.getNumOfClassFaculty(curr_id);
				int i = 0;
				int j = 0;
				int k = 0;
				JFrame tempFrame = new JFrame();
				JPanel modifyClassPanel = new JPanel(new GridLayout(
						classCount + 3, 1));
				JLabel explainLabel = new JLabel(
						"Please select the course you want to modify: ");
				JLabel explain2Label = new JLabel(
						"You can only modify the class you created! ");
				JButton[] listOfClassButton = new JButton[classCount];

				tempFrame.setSize(400, 200);
				tempFrame.setVisible(true);
				tempFrame.setLocationRelativeTo(null);
				tempFrame.setTitle("Purdue-CT");
				ArrayList<String> classNameAndID = p3.getClassName(curr_id);
				// final ArrayList<String> copyOfclassNameAndID =
				// p3.getClassName(curr_id);
				modifyClassPanel.add(explainLabel);
				modifyClassPanel.add(explain2Label);
				for (i = 0, j = i; i < classCount; i++)
				{
					listOfClassButton[i] = new JButton();
					final String tempClassID = classNameAndID.get(j);
					final String tempClassName = classNameAndID.get(j + 1);
					listOfClassButton[i].setText("Class ID: "
							+ classNameAndID.get(j) + "             "
							+ "Class name: " + classNameAndID.get(++j));
					j++;
					modifyClassPanel.add(listOfClassButton[i]);
					listOfClassButton[i].addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent arg0)
						{
							// Interface to modify a class
							final JFrame tempFrame = new JFrame();
							tempFrame.setSize(600, 200);
							tempFrame.setVisible(true);
							tempFrame.setTitle("Purdue-CT");
							tempFrame.setLocationRelativeTo(null);
							JLabel classNameLabel = new JLabel("Class name: ");
							JLabel semesterLabel = new JLabel("Semester: ");
							JLabel yearLabel = new JLabel("Year: ");
							JLabel meetsAtLabel = new JLabel("Meet At: (HH:MI)");
							JLabel roomLabel = new JLabel("Room: ");
							final JTextField classNameField = new JTextField();
							final JTextField semesterField = new JTextField();
							final JTextField yearField = new JTextField();
							final JTextField meetsAtField = new JTextField();
							final JTextField roomField = new JTextField();
							JPanel classPanel = new JPanel(new GridLayout(6, 2));
							JButton modifyButton = new JButton("Modify");
							JButton cancelButton = new JButton("Cancel");

							modifyButton.addActionListener(new ActionListener()
							{
								public void actionPerformed(ActionEvent e)
								{
									/*if (!classNameField.getText().equals(null)
											&& !semesterField.getText().equals(
													null)
											&& !yearField.getText()
													.equals(null)
											&& !meetsAtField.getText().equals(
													null)
											&& !roomField.getText()
													.equals(null))
									{*/
										if(p3.modifyClass(tempClassID,
												tempClassName,
												semesterField.getText(),
												yearField.getText(),
												meetsAtField.getText(),
												roomField.getText(), curr_id)== 1 ){
										JOptionPane
												.showMessageDialog(null,
														"Successfully modified a new class!");
										tempFrame.dispose();
										}else{
											JOptionPane
											.showMessageDialog(null,
													"Modify class failed, please check and try again!");
											
										}
									//}
								}
							});
							cancelButton.addActionListener(new ActionListener()
							{
								public void actionPerformed(ActionEvent e)
								{
									JOptionPane
											.showMessageDialog(null,
													"Successfully exit without modify a new class!");
									//tempFrame.dispose();
								}
							});
							classPanel.add(classNameLabel);
							classPanel.add(classNameField);
							classPanel.add(semesterLabel);
							classPanel.add(semesterField);
							classPanel.add(yearLabel);
							classPanel.add(yearField);
							classPanel.add(meetsAtLabel);
							classPanel.add(meetsAtField);
							classPanel.add(roomLabel);
							classPanel.add(roomField);
							classPanel.add(modifyButton);
							classPanel.add(cancelButton);
							tempFrame.add(classPanel);
							tempFrame.pack();
						}
					});
				}
				tempFrame.add(modifyClassPanel);
				tempFrame.pack();
			}
		});


		assignStudentButon.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				ArrayList<String> classNameAndID = p3.getClassName(curr_id);
				int classCount = p3.getNumOfClassFaculty(curr_id);
				final JFrame tempFrame = new JFrame();
				int i = 0;
				int j = 0;
				int k = 0;
				int checkstudentID = -1;
				tempFrame.setSize(600, 200);
				tempFrame.setVisible(true);
				tempFrame.setTitle("Purdue-CT");
				tempFrame.setLocationRelativeTo(null);

				JPanel modifyClassPanel = new JPanel(new GridLayout(
						classCount + 3, 1));
				JLabel explainLabel = new JLabel(
						"Please select the course you want to assign student to: ");
				JLabel explain2Label = new JLabel(
						"        You can only modify the class you created! ");
				JButton[] listOfClassButton = new JButton[classCount];
				modifyClassPanel.add(explainLabel);
				modifyClassPanel.add(explain2Label);
				
				for (i = 1, j = i; i < classCount; i++)  
				{
					listOfClassButton[i] = new JButton();
					final String tempClassID = classNameAndID.get(j);
					final String tempClassName = classNameAndID.get(j + 1);
					listOfClassButton[i].setText("Class name: " + classNameAndID.get(++j));
					
					
					/*
					listOfClassButton[i].setText("Class ID: "
							+ classNameAndID.get(j) + "             "
							+ "Class name: " + classNameAndID.get(++j));
					*/
					j++;
					modifyClassPanel.add(listOfClassButton[i]);

					listOfClassButton[i].addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent e)
						{
							System.out.println("ID : " + tempClassID);
							final JFrame tempFrame = new JFrame();
							tempFrame.setSize(600, 200);
							tempFrame.setVisible(true);
							tempFrame.setTitle("Purdue-CT");
							tempFrame.setLocationRelativeTo(null);
							JLabel studentIDLabel = new JLabel("Student ID: ");
							final JTextField studentIDField = new JTextField();
							JPanel addStudentPanel = new JPanel(new GridLayout(
									2, 2));
							final JButton registerStudentButton = new JButton(
									"Register");
							JButton cancelButton = new JButton("Cancel");
							registerStudentButton
									.addActionListener(new ActionListener()
									{
										public void actionPerformed(
												ActionEvent e)
										{
											if (p3.verifyStudentID(studentIDField
													.getText()) == -1)
											{
												JOptionPane
														.showMessageDialog(
																null,
																"This student does not exist in database, please check and try again!");
											} else if (!studentIDField
													.getText().equals(null)
													&& p3.verifyStudentID(studentIDField
															.getText()) == 1)
											{
												if(p3.registerStudent(
														tempClassName,
														studentIDField
																.getText(),tempClassID)==1 ){
												JOptionPane
														.showMessageDialog(
																null,
																"Successfully registered a new student!");
												tempFrame.dispose();
												}else{
													JOptionPane
													.showMessageDialog(null,
															"Register student error, please check the student ID");
												}
											}
										}
									});

							cancelButton.addActionListener(new ActionListener()
							{
								public void actionPerformed(ActionEvent e)
								{
									JOptionPane
											.showMessageDialog(null,
													"Successfully exit without register a new student!");
									tempFrame.dispose();
								}
							});
							addStudentPanel.add(studentIDLabel);
							addStudentPanel.add(studentIDField);
							addStudentPanel.add(registerStudentButton);
							addStudentPanel.add(cancelButton);
							tempFrame.add(addStudentPanel);
							tempFrame.pack();
						}
					});

				}
				tempFrame.add(modifyClassPanel);
				tempFrame.pack();
			}
		});

		createEvaluationButon.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				ArrayList<String> classNameAndID = p3.getClassName(curr_id);
				int classCount = p3.getNumOfClassFaculty(curr_id);
				final JFrame tempFrame = new JFrame();
				int i = 0;
				int j = 0;
				int k = 0;
				tempFrame.setSize(600, 200);
				tempFrame.setVisible(true);
				tempFrame.setTitle("Purdue-CT");
				tempFrame.setLocationRelativeTo(null);

				JPanel modifyClassPanel = new JPanel(new GridLayout(
						classCount + 3, 1));
				JLabel explainLabel = new JLabel(
						"Please select the course you want to create the evalution");
				JLabel explain2Label = new JLabel(
						"You can only create the evaluation on the class that you created! ");
				JButton[] listOfClassButton = new JButton[classCount];
				modifyClassPanel.add(explainLabel);
				modifyClassPanel.add(explain2Label);

				for (i = 1, j = i; i < classCount; i++)
				{
					listOfClassButton[i] = new JButton();
					final String tempClassID = classNameAndID.get(j);
					final String tempClassName = classNameAndID.get(j + 1);
					
					
					listOfClassButton[i].setText("Class name: " + classNameAndID.get(j));
					
					/*
					listOfClassButton[i].setText("Class ID: "
							+ classNameAndID.get(j) + "             "
							+ "Class name: " + classNameAndID.get(++j));
					
					*/
					j++;
					modifyClassPanel.add(listOfClassButton[i]);

					listOfClassButton[i].addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent e)
						{
							final JFrame tempFrame = new JFrame();
							tempFrame.setSize(600, 200);
							tempFrame.setVisible(true);
							tempFrame.setTitle("Create a evaluation");
							tempFrame.setLocationRelativeTo(null);
							JLabel evaluationTypeLabel = new JLabel("Type: ");
							JLabel evaluationWeightLabel = new JLabel(
									"Weight: ");
							JLabel evaluationDeadlineLabel = new JLabel(
									"Deadline: (dd,mm,yyyy) ");
							JLabel evaluationGradeLabel = new JLabel("Grade: ");
							JLabel evaluationStudentIDLabel = new JLabel(
									"Student ID: ");
							// JLabel evaluationClassNameLabel = new
							// JLabel("Class name: ");
							final JTextField evaluationTypeField = new JTextField();
							final JTextField evaluationWeightField = new JTextField();
							final JTextField evaluationDeadlineField = new JTextField();
							final JTextField evaluationGradeField = new JTextField();
							final JTextField evaluationStudentIDField = new JTextField();
							//Separate Panel
							final JPanel deadline = new JPanel();
							deadline.setLayout(new BoxLayout(deadline, BoxLayout.X_AXIS));
							//JComboBox Date
							final String[] d = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
							final String[] m = {"1","2","3","4","5","6","7","8","9","10","11","12"};
							final String[] y = {"2012","2013","2014","2015","2016","2017","2018","2019","2020","2021","2022","2023"};
							final JComboBox<String> day = new JComboBox<String>(d);
							final JComboBox<String> month = new JComboBox<String>(m);
							final JComboBox<String> year = new JComboBox<String>(y);
							deadline.add(day);
							deadline.add(month);
							deadline.add(year);
							// final JTextField evaluationClassNameField = new
							// JTextField();
							JButton createButton = new JButton("Create");
							JButton cancelButton = new JButton("Cancel");
							JPanel evaluationPanel = new JPanel(new GridLayout(
									6, 2));
							createButton.addActionListener(new ActionListener()
							{
								public void actionPerformed(ActionEvent e)
								{
									
									/*
									if (p3.verifyStudentID(evaluationStudentIDField
											.getText()) == -1)
									{
										JOptionPane
												.showMessageDialog(null,
														"This student does not exist in database, please check and try again!");
									} else if(p3.checkDate(Integer.parseInt(day.getSelectedItem().toString()), Integer.parseInt(month.getSelectedItem().toString()),Integer.parseInt(year.getSelectedItem().toString())) == 0){
										JOptionPane
										.showMessageDialog(null,
												"The date you entered is invalid. Please try again.");
									}else 
										
									
									if (!evaluationStudentIDField
											.getText().equals(null)
											&& p3.verifyStudentID(evaluationStudentIDField
													.getText()) == 1)
									{
									*/	
									
										String date = day.getSelectedItem() + "," + month.getSelectedItem() + "," + year.getSelectedItem();
										if(p3.createEvaluation(
												evaluationStudentIDField.getText(),
												evaluationTypeField.getText(),
												evaluationWeightField.getText(),
												date,
												evaluationGradeField.getText(),
												tempClassID, tempClassName, 
												curr_id,
												evaluationStudentIDField
														.getText())== 1 ){
										JOptionPane
												.showMessageDialog(
														null,
														"Successfully created a evaluaion for student: "
																+ evaluationStudentIDField
																		.getText());
										tempFrame.dispose();
										//}
										/*
										else {
											JOptionPane
											.showMessageDialog(null,
													"Create evaluation failed, please check and try again!");	
										}
										
										*/
									}
								}
							});
							cancelButton.addActionListener(new ActionListener()
							{
								public void actionPerformed(ActionEvent e)
								{
									JOptionPane
											.showMessageDialog(null,
													"Successfully exit without add an new evaluation!");
									tempFrame.dispose();
								}
							});
							evaluationPanel.add(evaluationStudentIDLabel);
							evaluationPanel.add(evaluationStudentIDField);
							evaluationPanel.add(evaluationTypeLabel);
							evaluationPanel.add(evaluationTypeField);
							evaluationPanel.add(evaluationWeightLabel);
							evaluationPanel.add(evaluationWeightField);
							evaluationPanel.add(evaluationDeadlineLabel);
							//evaluationPanel.add(evaluationDeadlineField);
							evaluationPanel.add(deadline);
							evaluationPanel.add(evaluationGradeLabel);
							evaluationPanel.add(evaluationGradeField);
							evaluationPanel.add(createButton);
							evaluationPanel.add(cancelButton);
							tempFrame.add(evaluationPanel);
							tempFrame.pack();
						}
					});
				}// end for loop
				tempFrame.add(modifyClassPanel);
				tempFrame.pack();
			}
		});
		modifyEvaluationButon.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				System.out.print("Found it 2");
				String tempPrevious = null;
				String temp2 = "HAHA";
				ArrayList<String> evaluationEidAndSid = new ArrayList<String>();
				final ArrayList<String> classNameAndID = p3.getEvaluationCidAndCname(curr_id);
				ArrayList<String> tempCID = new ArrayList<String>();
				for (int i = 0; i < classNameAndID.size(); i++)
				{
					String temp = classNameAndID.get(i).toString();
					if (!temp.equals(temp2))
					{
						tempCID.add(temp);
					}
					temp2 = temp;
				}
				System.out.println("tempCID : " + tempCID + " classNameAndID "
						+ classNameAndID);

				ArrayList<String> labelList = new ArrayList<String>();
				for (int i = 0; i < tempCID.size(); i++)
				{
					ArrayList<String> tempResult = new ArrayList<String>();
					String tempCname;
					String tempFid;
					String tempSID;
					String tempEID;
					tempResult = p3.getEvaluationEidAndSnum(curr_id,
							tempCID.get(i));
					// System.out.println(tempResult);
					for (int j = 0; j < tempResult.size(); j++)
					{
						tempCname = tempResult.get(j++);
						tempFid = tempResult.get(j++);
						tempSID = tempResult.get(j++);
						tempEID = tempResult.get(j);
						labelList.add(tempCname);
						labelList.add(tempFid);
						labelList.add(tempSID);
						labelList.add(tempEID);

					}
				}
				// System.out.println(labelList);

				// I have snum, eid, fid, labelList.size = 4
				// labelList: EDU100, 101, 418, 5000, EDU100, 101, 671, 5001,
				// EDU100, 101, 671, 5002, EDU100, 101, 418, 5012
				int classCount = labelList.size();
				int length = classCount / 4;
				final JFrame tempFrame = new JFrame();
				tempFrame.setSize(600, 200);
				tempFrame.setVisible(true);
				tempFrame.setTitle("Purdue-CT");
				tempFrame.setLocationRelativeTo(null);

				JPanel modifyClassPanel = new JPanel(new GridLayout(length + 2,
						1));
				JLabel explainLabel = new JLabel(
						"Please select the class evaluation you want to modify");
				JLabel explain2Label = new JLabel(
						"You can only modify the class evaluation that you created! ");
				JButton[] listOfClassButton = new JButton[length];
				modifyClassPanel.add(explainLabel);
				modifyClassPanel.add(explain2Label);
				System.out.println("length is: " + length);
				int i = 0;
				int j = 0;
				for (i = 0, j = i; i < length; i++)
				{
					listOfClassButton[i] = new JButton();
					System.out.println("j is:" + j);

					final String tempCname = labelList.get(j++);// 0;
					final String tempFid = labelList.get(j++);// 1
					final String tempSID = labelList.get(j++);// 2
					final String tempEID = labelList.get(j);// 3
					final String stringCID = classNameAndID.get(i);
					j++;
					listOfClassButton[i].setText("Class name: " + tempCname
							+ " ClassID: " + stringCID + ", FacultyID: "
							+ tempFid + ", StudentID: " + tempSID
							+ ", EvaluationID: " + tempEID);
					modifyClassPanel.add(listOfClassButton[i]);
					listOfClassButton[i].addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent e)
						{
							Date date = new Date();
							if (p3.checkDate(date, tempCname, tempFid, tempSID,
									tempEID, stringCID) == 1)
							{
								final JFrame tempFrame = new JFrame();
								tempFrame.setSize(600, 200);
								tempFrame.setVisible(true);
								tempFrame.setTitle("Modify a evaluation");
								tempFrame.setLocationRelativeTo(null);
								JLabel evaluationTypeLabel = new JLabel(
										"Type: ");
								JLabel evaluationWeightLabel = new JLabel(
										"Weight: ");
								JLabel evaluationDeadlineLabel = new JLabel(
										"Deadline: (dd,mm,yyyy) ");
								JLabel evaluationGradeLabel = new JLabel(
										"Grade: ");
								JLabel evaluationStudentIDLabel = new JLabel(
										"Student ID: ");
								JLabel evaluationStudentIDLabel2 = new JLabel(
										tempSID);
								// JLabel evaluationClassNameLabel = new
								// JLabel("Class name: ");
								final JTextField evaluationTypeField = new JTextField();
								final JTextField evaluationWeightField = new JTextField();
								final JTextField evaluationDeadlineField = new JTextField();
								final JTextField evaluationGradeField = new JTextField();
								// final JTextField evaluationStudentIDField =
								// new JTextField();
								// final JTextField evaluationClassNameField =
								// new JTextField();
								//Separate Panel
								final JPanel deadline = new JPanel();
								deadline.setLayout(new BoxLayout(deadline, BoxLayout.X_AXIS));
								//JComboBox Date
								final String[] d = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
								final String[] m = {"1","2","3","4","5","6","7","8","9","10","11","12"};
								final String[] y = {"2012","2013","2014","2015","2016","2017","2018","2019","2020","2021","2022","2023"};
								final JComboBox<String> day = new JComboBox<String>(d);
								final JComboBox<String> month = new JComboBox<String>(m);
								final JComboBox<String> year = new JComboBox<String>(y);
								deadline.add(day);
								deadline.add(month);
								deadline.add(year);
								//End date
								JButton modifyButton = new JButton("Modify");
								JButton cancelButton = new JButton("Cancel");
								JPanel evaluationPanel = new JPanel(
										new GridLayout(6, 2));
								modifyButton
										.addActionListener(new ActionListener()
										{
											public void actionPerformed(
													ActionEvent e)
											{
												String date = day.getSelectedItem() + "," + month.getSelectedItem() + "," + year.getSelectedItem();
												if(p3.checkDate(Integer.parseInt(day.getSelectedItem().toString()), Integer.parseInt(month.getSelectedItem().toString()),Integer.parseInt(year.getSelectedItem().toString())) == 0){
														JOptionPane
														.showMessageDialog(null,
																"The date you entered is invalid. Please try again.");
													} else if(p3.modifyEvaluation(tempEID,
														evaluationTypeField
																.getText(),
														evaluationWeightField
																.getText(),
														date,
														evaluationGradeField
																.getText(),
														tempCname, stringCID,
														tempFid, tempSID)==1 ){
												JOptionPane
														.showMessageDialog(
																null,
																"Successfully modified this evaluation!");
														tempFrame.dispose();
												}else{
													JOptionPane
													.showMessageDialog(null,
															"Modify evaluation failed, please check and try again!");
												}
											}
										});
								cancelButton
										.addActionListener(new ActionListener()
										{
											public void actionPerformed(
													ActionEvent e)
											{
												JOptionPane
														.showMessageDialog(
																null,
																"Successfully exit without modify an evaluation!");
												tempFrame.dispose();
											}
										});
								evaluationPanel.add(evaluationStudentIDLabel);
								evaluationPanel.add(evaluationStudentIDLabel2);
								evaluationPanel.add(evaluationTypeLabel);
								evaluationPanel.add(evaluationTypeField);
								evaluationPanel.add(evaluationWeightLabel);
								evaluationPanel.add(evaluationWeightField);
								evaluationPanel.add(evaluationDeadlineLabel);
								//evaluationPanel.add(evaluationDeadlineField);
								evaluationPanel.add(deadline);
								evaluationPanel.add(evaluationGradeLabel);
								evaluationPanel.add(evaluationGradeField);
								evaluationPanel.add(modifyButton);
								evaluationPanel.add(cancelButton);
								tempFrame.add(evaluationPanel);
								tempFrame.pack();
							} else
							{
								JOptionPane
										.showMessageDialog(null,
												"This evaluation deadline is eailer than today's date, can not modify this evaluation!");
							}
						}
					});
				}
				tempFrame.add(modifyClassPanel);
				tempFrame.pack();
			}
		});

		reportClassButon.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				int classCount = p3.getNumOfClassFaculty(curr_id);

				JFrame tempFrame = new JFrame();
				tempFrame.setSize(600, 200);
				tempFrame.setVisible(true);
				tempFrame.setTitle("Purdue-CT[Faculty]- Report of Classes");
				tempFrame.setLocationRelativeTo(null);
				JPanel reportPanel = new JPanel(new GridLayout(0, 5));

				System.out.println("classCount: " + classCount);
				JLabel classNameLabel = new JLabel("Class Name");
				JLabel meetsAtLabel = new JLabel("Meets at");
				JLabel roomLabel = new JLabel("Room");
				JLabel numOfStudentLabel = new JLabel("# of studnets");
				//JLabel numOfEvaluationLabel = new JLabel("# of evaluations");

				reportPanel.add(classNameLabel);
				reportPanel.add(meetsAtLabel);
				reportPanel.add(roomLabel);
				reportPanel.add(numOfStudentLabel);
				//reportPanel.add(numOfEvaluationLabel);

				ArrayList<String> reportOfClassInfo = p3.reportOfClass(curr_id);
				for (int i = 1; i < reportOfClassInfo.size(); i = i+2)
				{
					JLabel tempLabel = new JLabel(reportOfClassInfo.get(i));
					reportPanel.add(tempLabel);
				}
				tempFrame.add(reportPanel);
				//tempFrame.pack();
			}
		});

		reportStudentAndGradeButon.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				System.out.println(curr_id);
				ArrayList<String> studentAndGradesInfo = p3
						.getReportStudentAndGrade(curr_id);

				JFrame tempFrame = new JFrame();
				tempFrame.setSize(600, 200);
				tempFrame.setVisible(true);
				tempFrame
						.setTitle("Purdue-CT[Faculty]- Report of students and grades");
				tempFrame.setLocationRelativeTo(null);
				JPanel reportPanel = new JPanel(new GridLayout(0, 5));

				JLabel classNameLabel = new JLabel("Class Name");
				JLabel semesterLabel = new JLabel("Student ID");
				JLabel nameOfStudentLabel = new JLabel("Evaluation Type");
				JLabel yearLabel = new JLabel("Weight");
				JLabel currentGradeLabel = new JLabel("Current-grade");

				reportPanel.add(classNameLabel);
				reportPanel.add(semesterLabel);
				reportPanel.add(yearLabel);
				reportPanel.add(nameOfStudentLabel);
				reportPanel.add(currentGradeLabel);

				for (int i = 1; i < studentAndGradesInfo.size(); i = i+2)
				{
					JLabel tempLabel = new JLabel(studentAndGradesInfo.get(i));
					reportPanel.add(tempLabel);
				}
				tempFrame.add(reportPanel);
				//tempFrame.pack();
			}
		});

		exitButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				// TODO Auto-generated method stub
				//f1.dispose();
				//f2.dispose();
				try
				{
					//p3.conn.close();
					//p3.stmt.close();
					System.out.println("System successfully exit!");
				} catch (Exception e1)
				{
					// TODO Auto-generated catch block
					System.out
							.println("Database errors, did not close the connection");
				}
			}
			
			
			
		});
		facultyPanel.add(useridButton);
		facultyPanel.add(createClassButon);
		facultyPanel.add(modifyClassButon);
		facultyPanel.add(assignStudentButon);
		facultyPanel.add(createEvaluationButon);
		facultyPanel.add(modifyEvaluationButon);
		facultyPanel.add(reportClassButon);
		facultyPanel.add(reportStudentAndGradeButon);
		facultyPanel.add(exitButton);
		f2.add(facultyPanel);
		f2.pack();

	}

	public void studentInterface()
	{
		f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f2.setSize(500, 500);
		f2.setVisible(true);
		f2.setTitle("Purdue-CT");
		f2.setLocationRelativeTo(null);
		// useridLabel.setText();
		useridButton.setText("Welcome " + p3.getUserName(curr_id));
		studentPanel.add(useridButton);
		studentPanel.add(availableToolsButton);

		CalendarofevalutionsButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				JFrame tempFrame = new JFrame();
				tempFrame.setSize(600, 200);
				tempFrame.setVisible(true);
				tempFrame.setTitle("Purdue-CT[Student]- Calender of Classes");
				tempFrame.setLocationRelativeTo(null);
				JPanel studentClassReportPanel = new JPanel(
						new GridLayout(0, 3));

				//JLabel classNameLabel = new JLabel("Class Name");
				JLabel typeLabel = new JLabel("Evaluation type");
				JLabel deadLineLabel = new JLabel("DeadLine");

				//studentClassReportPanel.add(classNameLabel);
				studentClassReportPanel.add(typeLabel);
				studentClassReportPanel.add(deadLineLabel);

				ArrayList<String> studentCalenderReport = p3
						.getCalenderOfClasses(curr_id);
				for (int i = 1; i < studentCalenderReport.size(); i = i+2)
				{
					JLabel tempLabel = new JLabel(studentCalenderReport.get(i));
					studentClassReportPanel.add(tempLabel);
				}
				tempFrame.add(studentClassReportPanel);
				//tempFrame.pack();

			}
		});

		MyClassesButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				JFrame tempFrame = new JFrame();
				tempFrame.setSize(600, 200);
				tempFrame.setVisible(true);
				tempFrame.setTitle("Purdue-CT[Student]- Report of Classes");
				tempFrame.setLocationRelativeTo(null);
				JPanel studentClassReportPanel = new JPanel(
						new GridLayout(0, 5));

				// System.out.println("classCount: " + classCount);
				JLabel classNameLabel = new JLabel("Class Name");
				JLabel meetsAtLabel = new JLabel("Meets at");
				JLabel roomLabel = new JLabel("Room");
				JLabel SemesterLabel = new JLabel("Semester");
				//JLabel YearLabel = new JLabel("Year");

				studentClassReportPanel.add(classNameLabel);
				studentClassReportPanel.add(SemesterLabel);
				//studentClassReportPanel.add(YearLabel);
				studentClassReportPanel.add(meetsAtLabel);
				studentClassReportPanel.add(roomLabel);
				ArrayList<String> studentClassReport = p3
						.getStudentClass(curr_id);
				;
				for (int i = 1; i < studentClassReport.size(); i = i+2)
				{
					JLabel tempLabel = new JLabel(studentClassReport.get(i));
					studentClassReportPanel.add(tempLabel);
				}
				tempFrame.add(studentClassReportPanel);
				//tempFrame.pack();
			}
		});

		MygradesButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				JFrame tempFrame = new JFrame();
				tempFrame.setSize(600, 200);
				tempFrame.setVisible(true);
				tempFrame
						.setTitle("Purdue-CT[Student]- Report grades of Classes");
				tempFrame.setLocationRelativeTo(null);
				JPanel studentClassReportPanel = new JPanel(
						new GridLayout(0, 4));

				JLabel evaluationIDLabel = new JLabel("Evaluation ID");
				JLabel classNameLabel = new JLabel("Class Name");
				JLabel evaluationTypeLabel = new JLabel("Evaluation type");
				//JLabel gradeLabel = new JLabel("Grade");

				studentClassReportPanel.add(evaluationIDLabel);
				studentClassReportPanel.add(classNameLabel);
				studentClassReportPanel.add(evaluationTypeLabel);
				//studentClassReportPanel.add(gradeLabel);

				ArrayList<String> studentGradesReport = p3
						.getStudentGrades(curr_id);
				;
				for (int i = 1; i < studentGradesReport.size(); i = i+2)
				{
					JLabel tempLabel = new JLabel(studentGradesReport.get(i));
					studentClassReportPanel.add(tempLabel);
				}
				tempFrame.add(studentClassReportPanel);
				//tempFrame.pack();
			}

		});

		exitButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				// TODO Auto-generated method stub
				//f1.dispose();
				//f2.dispose();
				
				try
				{
					//p3.conn.close();
					//p3.stmt.close();
					System.out.println("System successfully exit!");
				} catch (Exception e1)
				{
					// TODO Auto-generated catch block
					System.out
							.println("Database errors, did not close the connection");
				}
			}
		});
		studentPanel.add(myMessageButton);
		studentPanel.add(CalendarofevalutionsButton);
		studentPanel.add(MyClassesButton);
		studentPanel.add(MygradesButton);
		studentPanel.add(exitButton);
		f2.add(studentPanel);
		f2.pack();
		// System.out.println(p3.getUserName(curr_id));
	}

	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		userInterface ui = new userInterface();
		ui.setInterface();
	}

}
